package njci.softerware.com.myexpress2017.serviceObj.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.List;

import njci.softerware.com.myexpress2017.serviceObj.ExpressUser;

public class ExpressUserDao {
    private Context context;
    private Dao<ExpressUser, Integer> userDaoOpe;
    private DatabaseHelper helper;

    public ExpressUserDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            userDaoOpe = helper.getDao(ExpressUser.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param user
     * @throws SQLException
     */
    public void add(ExpressUser user) {
        try {
            userDaoOpe.create(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public ExpressUser get(int id) {
        try {
            return userDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void delteExpressUser(ExpressUser user) {
        try {
            userDaoOpe.delete(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void updateExpressUser(ExpressUser user) {
        try {
            userDaoOpe.update(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<ExpressUser> findAllUser() {
        List<ExpressUser> list = null;
        try {
            list = userDaoOpe.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


    public ExpressUser getByUserId(Integer id) {
        try {
            List<ExpressUser> listUser = userDaoOpe.queryBuilder().where().eq("id", id).query();
            if(listUser!=null&&listUser.size()>0.){
              return   listUser.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }


}
